package com.handpay.zztong.hp.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;

/**
 * AvailableWithdrawMoneyDialog 查询用户当前可提现金额
 *
 * @author dlfeng
 */
public class WithdrawMoneyDialog extends Dialog {
    private Context mContext;
    private String money;
    private Button btOk;
    private MoneyEdit etAWMoney;
    private TextView tvMaxMoney;
    private TextView tvSHMoney;
    private View.OnClickListener pwdOnClickListener;
    private Double mMinWithdrawAmount = -1d;
    private Double mMaxWithdrawAmount = -1d;

    public WithdrawMoneyDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
        initView();
    }


    private void initView() {
        setCanceledOnTouchOutside(false);
        View layout = getLayoutInflater().inflate(R.layout.dialog_withdraw_money, null);
        layout.findViewById(R.id.btCancel).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        WithdrawMoneyDialog.this.dismiss();
                    }
                });
        tvMaxMoney = (TextView) layout.findViewById(R.id.tvMaxMoney);
        tvSHMoney = (TextView) layout.findViewById(R.id.tvSHMoney);
        btOk = (Button) layout.findViewById(R.id.btOk);
        etAWMoney = (MoneyEdit) layout.findViewById(R.id.etAWMoney);
        setListener();
        setContentView(layout);
        showDialog();
    }

    public void showDialog() {
        show();
        WindowManager windowManager = ((Activity) mContext).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth() - 100;
        getWindow().setAttributes(lp);

    }

    private void setListener() {
        btOk.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String toMoney = etAWMoney.getText().toString();
                if (TextUtils.isEmpty(toMoney)) {
                    Toast.makeText(mContext, "输入提现金额", Toast.LENGTH_LONG)
                            .show();
                    return;
                } else if (".".equals(toMoney)) {
                    Toast.makeText(mContext, "输入有效提现金额", Toast.LENGTH_SHORT).show();
                    return;
                }
                Double maxM = Double.parseDouble(money);
                Double toM = Double.parseDouble(toMoney);

                if ((toM * 100) < mMinWithdrawAmount
                        || (toM * 100) > mMaxWithdrawAmount) {
                    DialogFactory.showAlertDialog(mContext, mContext.getString(R.string.tip), mContext.getString(
                            R.string.tip_withdraw_alert,
                            mMinWithdrawAmount / 100, mMaxWithdrawAmount / 100));
                    return;
                }

                if (toM > maxM) {
                    Toast.makeText(mContext, "申请提现金额不能高于可提现余额", Toast.LENGTH_LONG).show();
                    return;
                }
                InputMethodManager imm = (InputMethodManager) mContext
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                if (pwdOnClickListener != null) {
                    v.setTag(toM);
                    pwdOnClickListener.onClick(v);
                    WithdrawMoneyDialog.this.dismiss();
                }

            }
        });
    }

    public void initResourse(Double maxTXMoney, Double minMoney, Double maxMoney, Double shMoney,
                             android.view.View.OnClickListener onClickListener) {
        pwdOnClickListener = onClickListener;
        mMinWithdrawAmount = minMoney;
        mMaxWithdrawAmount = maxMoney;
        String shm = CommonUtils.formatMoneyDouble(shMoney);
        this.money = CommonUtils.formatMoneyDouble(maxTXMoney / 100);
        if (!TextUtils.isEmpty(money)) {
            tvMaxMoney.setText(this.money + "元");
        }
        if (!TextUtils.isEmpty(shm)) {
            tvSHMoney.setText(shm + "元");
        }

    }
}
